﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Clothes.Items.ModuleData;
using ApplicationLibrary;
using System.Diagnostics;


namespace Clothes.Items.Scrounger
{
    internal class ItemsScrounger : ApplicationLibrary.DBManager.Scrounger
    {
        private const string ItemsSlave = "ITEMSSLAVE";
        private UserTabControl<TabItemsMaster> UtcItemsMaster = null;
        private TabItemsMaster tabItemsMaster = null;
        private Masters.Forms.Masters frmMaster;

        const string newItem = "NEWITEM";
        const string searchItems = "SEARCHITEMS";

        public ItemsScrounger(iScrounger frm)
            : base(frm)
        {
            this.frmMaster = (Masters.Forms.Masters)frm;
        }

        public override void InitializerComponent()
        {
            UtcItemsMaster = new UserTabControl<TabItemsMaster>(Properties.Resources.T_Items);
            UtcItemsMaster.Name = "UtcItemsMaster";
            tabItemsMaster = UtcItemsMaster.UserControl;
            frmMaster.MasterTabControl.Controls.Add(UtcItemsMaster);

            base.InitializerComponent();
        }

        public override void OnCustomizeToolbar(ToolStrip toolstrip)
        {
            guestForm.AddCustomButton(newItem, Properties.Resources.NewItem, Properties.Resources.B_Items, true);
            guestForm.AddCustomButton(searchItems, Properties.Resources.FindItem, Properties.Resources.B_Items, false);
            base.OnCustomizeToolbar(toolstrip);
        }

        public override bool OnEnableToolbarButtons(ToolStripButton sender, DBManager dbManager)
        {
            if (sender.Name == newItem)
                return dbManager.Count > 0;

            return base.OnEnableToolbarButtons(sender, dbManager);
        }

        public override void OnCustomButtonClick(ToolStripButton button)
        {
            if (button.Name == newItem)
            {
                Items.Forms.Items frmItems = GlobalInfo.OpenForm("Clothes.Clothes.Items.Forms.Items", false) as Items.Forms.Items;
                frmItems.LastCustomer = frmMaster.Customer.CounterNumericValue;
                frmItems.ToolbarEvent(DocumentForm.ToolbarEventKind.New);
            }
            if (button.Name == searchItems)
            {
                Items.Libraries.ItemSearcherForm frmSearch = new Libraries.ItemSearcherForm();
                frmSearch.ShowDialog();
            }
        }

        public override void OnAttachData(DBManager dbManager)
        {
            dbManager.AddSlave(ItemsSlave, false);
            tabItemsMaster.OnAttachData(dbManager);

            base.OnAttachData(dbManager);
        }

        public override void OnPrepareAuxData()
        {
            tabItemsMaster.OnPrepareAuxData();

            base.OnPrepareAuxData();
        }

        public override void OnBindData()
        {
            tabItemsMaster.OnBindData(guestForm);
            base.OnBindData();
        }

        public override string CreateSlaveQuery(string name)
        {
            QueryBuilder qb = new QueryBuilder();
            if (name == ItemsSlave)
            {
                qb.AddSelect();
                qb.AddFrom(CL_Items.Name);
                qb.AddCompare(CL_Items.Customer, "@p4");
                qb.AddCompare(CL_Items.Customer, "0", "!=");
            }
            return qb.Query;
        }

        public override List<SqlABParameter> CreateSlaveParam(string name)
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();
            if (name == ItemsSlave)
            {
                SqlABParameter p1 = new SqlABParameter("@p4", CL_Items.Customer);
                p1.Value = 0;
                PList.Add(p1);
            }
            return PList;
        }

        public override void SetParameters(DBManager dbManager, IRadarParameters key, DBCollection collection)
        {
            if (collection.Name == ItemsSlave)
            {
                collection.Parameter[0].Value = key[0];
            }
            base.SetParameters(dbManager, key, collection);            
        }
    }
}